<?php
/**
 * 产品品类模型
 * (model)
 */
namespace App\Models\Product;

use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Model;
class ProductClassify extends Model
{
    protected $table = 'product_classify';

    protected $guarded = [];

    const SEASON_SUMMER = 1; //夏季
    const SEASON_WINTER = 2; //冬季
    const SEASON_OTHER = 3;  //其他

    public static $_season = [
        self::SEASON_SUMMER => '夏季',
        self::SEASON_WINTER => '冬季',
        self::SEASON_OTHER  => '其他'
    ];

    const SYNC_YES  = 1; //启用
    const SYNC_NO = 2; //停用
    public static $_status = [
        self::SYNC_YES  => '启用',
        self::SYNC_NO   => '停用'
    ];

    public static $_reportStatus = [
        1 => '开放',
        2 => '关闭'
    ];

    public static function options($where = [])
    {
        $query = $where ? self::query()->where([$where]) : self::query();
        $res = $query->where('id','>',0)->get()->map(function ($model){
            return collect(['title' => $model->title.'('.$model->model_no.')',
                'id' => $model->id]);
        });

        return $res->pluck('title','id');
    }

    public function product_ppt(){
        return $this->hasOne(ProductPpt::class,'classify_id');
    }

    public function quality(){
        return $this->hasOne(ProductQuality::class,'classify_id');
    }

    public function package_material(){
        return $this->hasOne(ProductPackageMaterial::class,'classify_id');
    }

    public function sku(){
        return $this->hasMany(ProductSku::class, 'classify_id', 'id');
    }

    public static function getTitleById($id)
    {
        return self::query()->where('id',$id)->value('title');
    }

    public static function getSeasonById($id)
    {
        return self::query()->where('id',$id)->value('season');
    }

    public static function getIdByTitle($title)
    {
        return self::query()->where('title',$title)->value('id');
    }

    public static function getFiles($value)
    {
        if(!$value)return '<label class="label label-warning">未上传文件</label>';
        if ($value instanceof Arrayable) {
            $value = $value->toArray();
        }
        return collect((array) $value)->filter()->map(function ($path){
            $name = basename($path);
            $html = <<<HTML
            <label class="label label-success">{$name}</label>
HTML;
            return $html;
        })->implode('<br/>');
    }

    /**
     * 获取品类
     * @param $id
     * @return \Illuminate\Database\Eloquent\Builder|Model|object|null
     */
    public static function getModelById($id){
        return self::query()->where('id', $id)->first();
    }

}
